﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class user_formlogin : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
            ReadCookie();
    }

    private void WriteCookie(String Email, String Pass)
    {
        HttpCookie httpCookie = new HttpCookie("Remember");
        httpCookie.Values.Add("Username", Email);
        httpCookie.Values.Add("Password", Pass);
        Response.Cookies.Add(httpCookie);
    }
    private void ReadCookie()
    {
        if (Request.Cookies["Remember"] != null)
        {
            HttpCookie httpCookie = Request.Cookies["Remember"];
            txtUser.Text = httpCookie.Values["Username"].ToString();
            txtPass.Text = httpCookie.Values["Password"].ToString();
        }
    }
    protected void imglogin_Click(object sender, ImageClickEventArgs e)
    {
        String _email = txtUser.Text.Trim();
        String _password = txtPass.Text.Trim();
        CustomerBus CustomerBus = new CustomerBus();

        AdminBus AdminBus = new AdminBus();
        Admin objAdmin = new Admin();
        objAdmin.AdmUser = _email;
        objAdmin.AdmPass = _password;
        if (CustomerBus.checkLogin(_email, _password))
        {
            Session["login"] = "ok";
            Session.Add("email", _email);
            Session.Add("password", _password);
            if (CheckBoxRemember.Checked)
            {
                WriteCookie(_email, _password);
            }
            Response.Redirect("default.aspx");

        }
        else if (AdminBus.checkAdminLogin(objAdmin))
        {
            Session["adminlogin"] = "Administrator";
            Session.Add("emailadmin",_email);
            Response.Redirect("default.aspx");
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Messages", "<script>alert('Login faild!')</script>", false);
        }
    }
}